home *** CD-ROM | disk | FTP | other *** search
/ PC-X 1997 October / pcx14_9710.iso / swag / files.swg / 0095_Create plain ASCII data from binary file.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1996-11-29  |  6.3 KB  |  172 lines

  1.  
  2. PROGRAM BinToInclude; (* Version 1.0, 12.10.96 *)
  3.  
  4. (*
  5.  
  6.    Author: Dirk "Rob!" Schwarzmann, (w) September 1996, Germany
  7.  
  8.    E-Mail: rob@rbg.informatik.th-darmstadt.de
  9.  
  10.    WWW   : http://www.student.informatik.th-darmstadt.de/~rob/
  11.  
  12.  
  13.    About this small piece of code:
  14.  
  15.    This program reads any type of file and writes it back as a include
  16.    file  (text type for TurboPascal) where every byte read is written as
  17.    its value. So, if a .com file is read and byte #x is an @, BinToInclude
  18.    will write the number 64 for it.
  19.  
  20.    In addition, it creates a header so that you can use the new data
  21.    file directly as an include file in your own programs. The binary file
  22.    is stored as an array of byte and you can write it back to disc by using
  23.    the small procedure shown below.
  24.  
  25.    What's it all good for?
  26.  
  27.    You can hold binary files of _any_ type in your own program and do not
  28.    have to take care that an external data (or program) file is truly there.
  29.    If it does not exist, you simple write it! No panic, if a user has
  30.    accidently deleted a file or a file is corrupted!
  31.  
  32.    Using this program:
  33.  
  34.    You have to specify at least one command line parameter giving the file
  35.    you like to read. If no second parameter (the target file name) is given,
  36.    BinToInclude will create an .INC file with the path and name similar to
  37.    the source file. Otherwise the given name is used.
  38.    Note that BinToInclude does very little error checking! It does not
  39.    assure that the source file exists nor does it prevent you from over-
  40.    writing existing include files! But because this program is only a small
  41.    quick-hacked utility for programmers, I guess this not very important.
  42.    If you include a file in your program, keep in mind that you may get
  43.    problems if you include files bigger than 64k!
  44.  
  45.    There a some constants you can only change in this source code;
  46.  
  47.     - COMPRESSED is a boolean flag and toggles the writing of data between
  48.       the well-to-read format:
  49.         001, 020, 234, 089, ...
  50.       and the space-saving format:
  51.         1, 20, 234, 89, ...
  52.       If you want to save even the blanks between comma and numbers, get
  53.       your editor, mark the whole area and do a search + replace. It's
  54.       that simple! It would have taken much more effort to do this here.
  55.  
  56.     - ArrayFormat
  57.       Specify the number of rows the array definition should use. 65 - 70
  58.       rows are a good choice to keep a good readability.
  59.  
  60.     - Ext2_Str
  61.       Here you can change the default data file suffix - but I think there
  62.       is no need to do so.
  63.  
  64.    To write the data back in a file, you only need this small routine:
  65.  
  66.      PROCEDURE WriteArrayToFile(TargetName:FILE OF BYTE);
  67.  
  68.      VAR
  69.        i : LONGINT;
  70.  
  71.      BEGIN
  72.        Assign(TargetFile,TargetName);
  73.        Rewrite(TargetFile);
  74.        FOR i := 1 TO BinFileSize DO
  75.          Write(TargetFile,BinFile[i]);
  76.        Close(TargetFile);
  77.      END;
  78.  
  79.    That's all!
  80.  
  81.    For any suggestions, please feel free to email me!
  82.  
  83.  *)
  84.  
  85. USES DOS;
  86.  
  87. CONST
  88.   Compressed : BOOLEAN = FALSE; (* False: 010, 009, 255, ...
  89.                                    True: 10, 9, 255,...
  90.                                    If you want to have 10,9,255 you can
  91.                                    remove the blanks with search +
  92. replace
  93.                                    in your editor! *)
  94.  
  95.   ArrayFormat : BYTE = 65; (* The width of the array definition area
  96.                               (number of rows) *)
  97.  
  98.   Ext2_Str : ExtStr = '.INC'; (* The default suffix for the file to write *)
  99.  
  100.   (* These lines are the header of the written include-file. After the
  101.      variable "BinFileSize =" the program will insert the file length
  102.      (=array length) and after the last header line the data will  follow. *)
  103.  
  104.   IncHeader1 : STRING = 'CONST';
  105.   IncHeader2 : STRING = '  BinFileSize = ';
  106.   IncHeader3 : STRING = '  BinFile : ARRAY[1..BinFileSize] OF BYTE = (';
  107.  
  108. VAR (* main.BinToInclude *)
  109.   SourceFile : FILE OF BYTE;
  110.   TargetFile : TEXT;
  111.   SourceName : STRING[128];
  112.   TargetName : STRING[128];
  113.   SourceByte : BYTE;
  114.   TgtByteStr : STRING[5];
  115.   TargetStr : STRING[80];
  116.   Dir_Str : DirStr;
  117.   Name_Str : NameStr;
  118.   Ext_Str : ExtStr;
  119.  
  120. BEGIN (* main.BinToInclude *)
  121.   (* The case statement is only to parse the command line: *)
  122.   CASE ParamCount OF
  123.     1: BEGIN
  124.       FSplit(FExpand(ParamStr(1)),Dir_Str,Name_Str,Ext_Str);
  125.       SourceName := Dir_Str + Name_Str + Ext_Str;
  126.       TargetName := Dir_Str + Name_Str + Ext2_Str;
  127.     END; (* case ParamCount of 1 *)
  128.     2: BEGIN
  129.       FSplit(FExpand(ParamStr(1)),Dir_Str,Name_Str,Ext_Str);
  130.       SourceName := Dir_Str + Name_Str + Ext_Str;
  131.       FSplit(FExpand(ParamStr(2)),Dir_Str,Name_Str,Ext_Str);
  132.       TargetName := Dir_Str + Name_Str + Ext_Str;
  133.     END; (* case ParamCount of 2 *)
  134.   ELSE (* case ParamCount *)
  135.     WriteLn('Please specify at least one Parameter as the source file.');
  136.     Write('If the optional second one is not given, <Source file>.INC is');
  137.     WriteLn(' assumed.');
  138.     Halt(1);
  139.   END; (* case ParamCount *)
  140.  
  141.   Assign(SourceFile,SourceName);
  142.   Reset(SourceFile);
  143.   Assign(TargetFile,TargetName);
  144.   Rewrite(TargetFile);
  145.   WriteLn(TargetFile,IncHeader1);
  146.   WriteLn(TargetFile,IncHeader2,FileSize(SourceFile),';');
  147.   WriteLn(TargetFile,IncHeader3);
  148.   TargetStr := '    '; (* Set the left margin *)
  149.   Inc(ArrayFormat,2); (* This needs an explanation: because of the 4 blanks
  150.                          on the left margin, we should add 4 to ArrayFormat.
  151.                          But as every number will be followed by a comma
  152.                          and a blank, we have to decrease it by 2. -> add 2 *)
  153.   WHILE NOT EoF(SourceFile) DO BEGIN
  154.     Read(SourceFile,SourceByte);
  155.     Str(Ord(SourceByte),TgtByteStr);
  156.     IF NOT Compressed THEN
  157.       TgtByteStr := Copy('00',1,3-Length(TgtByteStr)) + TgtByteStr;
  158.     IF (Length(TargetStr) + Length(TgtByteStr) > ArrayFormat) THEN BEGIN
  159.       WriteLn(TargetFile,TargetStr); (* Flush the string *)
  160.       TargetStr := '    ';
  161.     END;
  162.     TargetStr := TargetStr + TgtByteStr + ', ';
  163.   END; (* while not EoF(SourceFile) *)
  164.   (* Flush the buffer string but don't write the last comma: *)
  165.   Write(TargetFile,Copy(TargetStr,1,Length(TargetStr)-2));
  166.   WriteLn(TargetFile,');'); (* Close the array definition with the ")"
  167.   *)
  168.   Close(TargetFile);
  169.   Close(SourceFile);
  170. END. (* main.BinToInclude *)
  171.  
  172.